import java.util.StringTokenizer;


class P11561_39643 {

	public static void main(String[] args) {
		String line = readLn();
		
		while (line != null) {
			StringTokenizer st = new StringTokenizer(line);
			
			int columns = Integer.parseInt(st.nextToken());
			int rows = Integer.parseInt(st.nextToken());
			
			char[][] map = new char[rows][columns];
			int playerX = 0, playerY = 0;
			
			for (int i = 0; i < rows; i++) {
				line = readLn();
				
				for (int j = 0; j < columns; j++) {
					map[i][j] = line.charAt(j);
					if (map[i][j] == 'P') {
						playerX = j;
						playerY = i;
					}
						
				}
				
			}
			
			int golds = findGold(map, playerX, playerY);

			System.out.println(golds);
			
			line = readLn();
		}
	}
	
	private static int findGold(char[][] map, int playerX, int playerY) {
		if (map[playerY][playerX] == 'T' || map[playerY][playerX] == '#')
			return 0;
		
		int count = 0;
		
		if (map[playerY][playerX] == 'G')
			count++;
		
		map[playerY][playerX] = '#';
		
		if (map[playerY][playerX + 1] != 'T' &&
			map[playerY + 1][playerX] != 'T' &&
			map[playerY][playerX - 1] != 'T' &&
			map[playerY - 1][playerX] != 'T') {
		
			count += findGold(map, playerX, playerY + 1);
			count += findGold(map, playerX + 1, playerY);
			count += findGold(map, playerX, playerY - 1);
			count += findGold(map, playerX - 1, playerY);
		}
		return count;
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuilder buffer = new StringBuilder();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString());
	}
	
}
